home *** CD-ROM | disk | FTP | other *** search
- #----------------------------------------------------------------------------------------------------------------------------------------------------
- # ReplaceAll
- # MPW Shell Script
- # Written by Gina Cherry • August 16, 1991
- # Copyright: © 1991 by Apple Computer, Inc., all rights reserved.
- #
- # Usage:
- # ReplaceAll selection newString name...
- #
- # Function:
- # ReplaceAll replaces all instances of the selection with the string newString in all files specified
- # on the command line. The selection must be a regular expression. If a directory is given
- # as a parameter, ReplaceAll replaces the selection with the new string in all files contained
- # in the directory and all of its subdirectories.
- #
- # Note:
- # The parameters must be in the order shown above.
- # Directory names must end with a ':'.
- # Tag variables cannot be used with ReplaceAll
- # ReplaceAll may be used with the Commando option.
- #----------------------------------------------------------------------------------------------------------------------------------------------------
-
- # Don't exit program on error.
- Set Exit 0
-
- # Make sure there are at least 3 parameters.
- If "{#}" < 3
- Echo "### {0}: too few parameters"
- Echo "### Usage: {0} selection newstring files..."
- Exit 1
- End
-
- # Save pattern to find and replace in Old.
- Set Old "{1}"
-
- # Save string to replace it with in New.
- Set New "{2}"
-
- # Shift parameters by 2 so that parameters are now all file names or directories.
- Shift 2
-
-
- # Loop through remaining parameters.
- For item in {"parameters"}
-
- # Make sure each parameter is a valid file or directory. If an invalid file or directory was given,
- # write an error message and continue processing parameters.
- If !"`Exists "{item}"`"
- Echo "### {0}: {item} not a valid file or directory name."
- Continue
- End >> Dev:StdErr
-
- # List the full path name if the parameter is a text file, or list the full path names of all text files
- # in the directory and its subdirectories if the parameter is a directory. Discard diagnostic
- # output in bit bucket.
- Set files "`Files -t TEXT -r -s -f "{item}" ≥ Dev:Null`"
-
- # Loop through files in the list.
- For fName in {files}
- # Make file the active file.
- Open "{fName}" ≥ Dev:Null
-
- Continue If {Status} != 0
-
- # Position cursor at the top of the file.
- Find • "{fName}"
-
- # Replace all instances of the selection with the new string.
- Replace -c ∞ "{Old}" "{New}" "{fName}"
-
- # Close the file; save changes.
- Close -y "{fName}"
- End
- End